package com.quasar.hpatient.module.home_medicine_today;

import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.bean.home_medicine.MedicineTodayBean;
import com.quasar.hpatient.dialog.AlertDialog;
import com.quasar.hpatient.dialog.CalendarDialog;
import com.quasar.hpatient.module.home_medicine_edit.MedicineEditActivity;

import lib.quasar.base.frame.BaseActivity;
import lib.quasar.context.BaseConstant;
import lib.quasar.widget.chart.ChartView;

/**
 * description: 当日服药记录
 * created by kalu on 2018/5/10 8:43
 */
public class MedicineTodayActivity extends BaseActivity<MedicineTodayPresenter> implements MedicineTodayView {

    private RecyclerView recycler ;
    @Override
    public int initView() {
        return R.layout.activity_home_medicine_oral;
    }

    @Override
    public void initDataLocal() {

        findViewById(R.id.activity_home_medicine_oral_back).setOnClickListener(v -> onBackPressed());
        //findViewById(R.id.activity_home_medicine_oral_add).setOnClickListener(v -> goNext(MedicineEditActivity.class));

        recycler = findViewById(R.id.activity_home_medicine_oral_list);
        getPresenter().initList(this, recycler);

    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void initDataNet() {

        //final RecyclerView recycler = findViewById(R.id.activity_home_medicine_oral_list);
        getPresenter().recordList(this, recycler, BaseConstant.NULL_STR);
    }

    @Override
    public void showCalendar() {

//        CalendarDialog dialog = new CalendarDialog(this);
//       /* dialog.setOnCalendarChangeListener((date, time) -> {
//            final RecyclerView recycler = findViewById(R.id.activity_home_medicine_oral_list);
//            getPresenter().recordList(MedicineTodayActivity.this, recycler, time);
//            getPresenter().setTime(MedicineTodayActivity.this, recycler, date);
//        });*/
//        //final RecyclerView recycler = findViewById(R.id.activity_home_medicine_oral_list);
//        dialog.setOnCalendarChangeListener((years, months, days, maxDay, isClick) -> getPresenter().setTime(MedicineTodayActivity.this, recycler, years + "-" + months + "-" + days));
//        dialog.show();

        final CalendarDialog dialog = new CalendarDialog(this);
        dialog.setScroll(getDate());
        dialog.show();
        dialog.setOnCalendarChangeListener((years, months, days, maxDay, isClick) -> {
            if (isClick) {
                final TextView date =getPresenter().getHeadDateView();
                setText(date, years + "-" + months + "-" + days);
                final RecyclerView recycler = findViewById(R.id.activity_home_medicine_oral_list);
                //final ChartView chart = findViewById(R.id.activity_home_bloodsugar_chart);
                //final boolean mulit = getIntent().getBooleanExtra(MULIT, false);
                //final TextView date1 = findViewById(R.id.activity_home_bloodsugar_date);
                getPresenter().recordList(this,recycler,getDate());
            } else {
                getPresenter().schemelList(dialog, years, months, days, maxDay);
            }
        });
    }

    @Override
    public String getDate() {
        final TextView text = getPresenter().getHeadDateView();
        return text.getText().toString();
    }


    @Override
    public void showWarning() {
        if (getPresenter().isDoseAll()){
            toast("药品已经全部服用！");
            return;
        }

        final AlertDialog dialog = new AlertDialog(this);
        dialog.show();
        dialog.setMiddleGone();
        dialog.setTopTitles("确定药物已全部服用？");
        dialog.setTitles("注：确认后，\r\n您还可以进行详细修改！");
        dialog.setButton("否", "是");
        dialog.setOnDialogChangeListener((left, right) -> {
            if (left) {


            } else if (right) {

                getPresenter().editMedicineOral(this,recycler,-1,4);
            }
        });
    }


    @Override
    public void showHint(int position,int flag) {

        MedicineTodayBean.ItemBean bean = getPresenter().getBean(position);
        if (null == bean)
            return;
        String unit = bean.getDose_name();
        String[] split = unit.split("/");
        String unitend = "";
        if (null != split && split.length == 2) {
            unitend+=split[1];
        }
        String title = "药品名称："+bean.getMname()+"\r\n"+"药品规格："+unit+"\r\n"+"本次用量："+bean.getUse_number()+unitend+"\r\n"+"应服药时间："+bean.getMedicineTime();
        final AlertDialog dialog = new AlertDialog(this);
        dialog.show();
        switch (flag){
            case -1:
                dialog.setTopTitles("你确定要服药了吗 ？(你已提前)");
                break;
            case 1:
                dialog.setTopTitles("你确定要服药了吗 ？(你已超时)");
                break;
            case 0:
            default:
                dialog.setTopTitles("你确定要服药了吗 ？");
                dialog.setMiddleGone();
        }

        dialog.setTitles(title);
        dialog.setButton("否", "是");
        dialog.setOnDialogChangeListener((left, right) -> {
            if (left) {


            } else if (right) {

                getPresenter().editMedicineOral(this,recycler,position,1);
            }else{
                getPresenter().editMedicineOral(this,recycler,position,3);
            }
        });
    }
}
